We will use data visualization to answer the following question:
Do cars with big engines use more fuel than cars with small engines?
Add useful titles and labels
ggplot(data = mpg, mapping =aes(x = displ, y = hwy)) +geom_point(mapping =aes(colour = class)) +geom_smooth(method ="lm") +labs(title ="Engine displacement and highway miles per gallon",subtitle ="Values for seven different classes of cars",x ="Engine displacement (L)",y ="Highway miles per gallon" ) +scale_color_colorblind()
Add useful titles and labels
Flexible visualization
You can use visual elements to communicate your findings in engaging ways.
ggplot(data = mpg) +geom_point(mapping =aes(x = displ, y = hwy, color = class =="2seater"))
ggplot(data = mpg, mapping =aes(x = displ, y = hwy)) +geom_point(mapping =aes(colour = class)) +geom_smooth(method ="lm") +theme_minimal() +labs(title ="Engine displacement and highway miles per gallon",subtitle ="Values for seven different classes of cars",x ="Engine displacement (L)",y ="Highway miles per gallon" ) +scale_color_colorblind()
ggplot(data = mpg, mapping =aes(x = displ, y = hwy)) +geom_point(mapping =aes(colour = class)) +geom_smooth(method ="lm") +theme(legend.position ="bottom",panel.grid =element_blank(),panel.background =element_blank(),plot.title.position ="plot",plot.title =element_text(face ="bold") ) +labs(title ="Engine displacement and highway miles per gallon",subtitle ="Values for seven different classes of cars",x ="Engine displacement (L)",y ="Highway miles per gallon" ) +scale_color_colorblind()
Creating your own theme
The before shot
EXERCISE
Customize the last plot you made using the theme() argument.
Working with categorical data
We often want to explore patterns in categorical (or discrete) data. We need new tools to do this.
select(mpg, manufacturer, model, drv)
# A tibble: 234 × 3
manufacturer model drv
<chr> <chr> <chr>
1 audi a4 f
2 audi a4 f
3 audi a4 f
4 audi a4 f
5 audi a4 f
6 audi a4 f
7 audi a4 f
8 audi a4 quattro 4
9 audi a4 quattro 4
10 audi a4 quattro 4
# ℹ 224 more rows